#!/usr/bin/perl -w

use Test::More tests => 5;

use File::Temp;

# =============================================================
# Cargamos el modulo
# ==============================================================
use_ok('Bench::Test::View');

# =============================================================
# Comprobamos que la creacion del objeto es correcta
# =============================================================
my $view = Bench::Test::View->new(
   format  => 'text',
   verbose => 0,
);

isa_ok($view, 'Bench::Test::View');

my @rows = (
   ['ONE', 'TWO', 'THREE', 'FOUR'],
   [    1,     2,       3,      4],
   [    5,     6,       7,      8]
);

$view->add_row($_) for @rows;

my $textoutput = <<END_TEXT;
ONE  TWO  THREE  FOUR  
1    2    3      4     
5    6    7      8     
END_TEXT

is($view->process, $textoutput, 'Text template ok');

my $htmloutput = <<END_HTML;
<html>
   <head>
      <style type="text/css">
         table {
            text-align: center;
            font-family: tahoma, sans-serif;
            font-size: 0.7em;
            color: #777;
            margin-bottom: 1.5em;
         }
         table th {
            font-weight: bold;
            padding: 0.5em;
            color: #444;
            background-color: #d0d0d0;
         }
         table td {
            padding: 0.5em;
         }
         table tr.rowA {
            background-color: #efefef;
         }
         table tr.rowB {
            background-color: #fafafa;
         }
      </style>
   </head>
   <body>
      <table>
         <tr>
            <th>ONE</th>
            <th>TWO</th>
            <th>THREE</th>
            <th>FOUR</th>
         </tr>
         <tr class=rowA>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
         </tr>
         <tr class=rowB>
            <td>5</td>
            <td>6</td>
            <td>7</td>
            <td>8</td>
         </tr>
      </table>
   </body>
</html>
END_HTML

is($view->process('html'), $htmloutput, 'HTML template ok');

my $usertemplate = <<TEMPLATE;
[%- FOREACH row IN data -%]
   [%- FOREACH item IN row -%]([%- item -%])[%- END %]
[% END -%]
TEMPLATE

my ($fh, $tmpfile) = File::Temp::tempfile;

print $fh "$usertemplate\n";
close $fh or die "An error ocurred when trying to close $tmpfile\n";

my $useroutput = <<END_OUT;
(ONE)(TWO)(THREE)(FOUR)
(1)(2)(3)(4)
(5)(6)(7)(8)

END_OUT

is($view->process("file:$tmpfile"), $useroutput, 'User template ok');
